package logic

import (
	"context"
	"fmt"
	"jingdong_product/models/mysql"

	"jingdong_product/productrpc/internal/svc"
	"jingdong_product/productrpc/productrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProductAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewProductAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductAddLogic {
	return &ProductAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 开发商品信息
func (l *ProductAddLogic) ProductAdd(in *productrpc.ProductAddRequest) (*productrpc.ProductAddResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	mysql.DB.Debug().Where("id = ?", in.UserId).Find(&user)
	if user.ID == 0 {
		return nil, fmt.Errorf("该商家不存在，请重新选择")
	}

	product := mysql.Product{
		Title:  in.Title,
		Price:  float64(in.Price),
		Info:   in.Info,
		Image:  in.Image,
		UserId: in.UserId,
	}
	err := mysql.DB.Debug().Create(&product).Error
	if err != nil {
		return nil, fmt.Errorf("商品信息更新失败")
	}
	return &productrpc.ProductAddResponse{
		Id: int64(product.ID),
	}, nil
}
